package org.itboys.ace.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.baseobject.entity.AdvertisingSC;
import org.itboys.baseobject.service.AdvertisingSCService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.entity.Product;
import org.itboys.product.entity.ProductCategory;
import org.itboys.product.service.ProductCategoryService;
import org.itboys.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


/**
 * 广告管理
 * 
 * @author Administrator
 * 
 */

@RestController
@RequestMapping("/AdvertisingSCController")
public class AdvertisingSCController extends BaseController {

	@Autowired
	private AdvertisingSCService advertisingSCService;
	@Autowired
	private ResourceHolder resourceHolder;
	@Autowired
	private BusinessCenterUserService businessCenterUserService;
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductCategoryService productCategoryService;

	@RequestMapping("/toVM")
	public ModelAndView toVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/adve/advertisingSC");
	}

	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		Long centerId = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId()).getCenterId();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("centerId", centerId);
		Page<AdvertisingSC> page = advertisingSCService.pageQuery(param, request);
		for(AdvertisingSC advertising : page.getData()) {
			advertising.setImage(resourceHolder.getStringValue("staticRoot")+"/upload" + advertising.getImage());
		}
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	@RequestMapping("/getById/{id}")
	public void getById(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) {
		AdvertisingSC advertising = advertisingSCService.getById(id);
		advertising.setImage(resourceHolder.getStringValue("staticRoot")+"/upload" + advertising.getImage());
		AjaxUtils.renderJson(response, advertising);
	}

	@RequestMapping("/save")
	public void save(@ModelAttribute AdvertisingSC advertising, @RequestParam("file") MultipartFile file, HttpServletResponse response) {
		try {
			Long centerId = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId()).getCenterId();
			advertising.setCenterId(centerId);
			if(advertising.getDetailType() != 2) { //不是分类
				Product product = productService.getById(advertising.getDetailId());
				if(product == null) {
					AjaxUtils.renderJson(response, "-2");
					return ;
				}
			} else {
				ProductCategory info = productCategoryService.getById(advertising.getDetailId());
				if(info == null) {
					AjaxUtils.renderJson(response, "-2");
					return ;
				}
			}
			if (file != null && !file.isEmpty()) {
				String filePath = FileUtils.saveFile(file.getInputStream(), resourceHolder.getStringValue("fileUploadPath"), file.getOriginalFilename(), file.getContentType());
				advertising.setImage(filePath);
			}
			if (advertising.getId() == 0L) {
				if(file == null || file.isEmpty()) {
					AjaxUtils.renderJson(response, "-1");
					return ;
				}
				advertisingSCService.save(advertising);
			} else {
				AdvertisingSC ad = advertisingSCService.getById(advertising.getId());
				if(file != null && !file.isEmpty()) {
					ad.setImage(advertising.getImage());
				}
				ad.setDesc(advertising.getDesc());
				ad.setDetailId(advertising.getDetailId());
				ad.setDetailType(advertising.getDetailType());
				ad.setType(advertising.getType());
				advertisingSCService.update(ad);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("phone save error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable("id") Long id, HttpServletResponse response) {
		try {
			advertisingSCService.delete(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("appInfo delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
